﻿package engine.base.sim {
	
	import flash.utils.Dictionary;
	
	/**
	* A SimSet represents a collection of SimObjects
	* @author Jon Smelquist
	*/
	public class SimSet extends SimObject{
		
		protected var _objects:Dictionary;
		
		public function SimSet()
		{
			_objects = new Dictionary();
		}
		
		public function getChildren():Dictionary {
			return _objects;
		}
		
		/**
		 * Find the object that is stored under the specified key.
		 * @param	name	The key of the object.
		 * @return SimObject associated with the given key.
		 */
		public function findObject(name:String):SimObject
		{
			if (!_objects.hasOwnProperty(name))
				throw new Error("No Object found!");
			return _objects[name];
		}
		
		/**
		 * Add an object to the SimSets children under a specified key.
		 * Objects can be added without a key if there is no desire to 
		 * search for those objects later.
		 * @param	obj		Object to be added to the children
		 * @param	name	Key to store object under
		 */
		public function addObject(obj:SimObject, name:String = null):void
		{
			if (_objects.hasOwnProperty(name))
				throw new Error("Object with that key already exists!");
			
			if (name == null)
				_objects[obj] = obj;
			else
				_objects[name] = obj;
		}
		
		/**
		 * Replace the object with the current key "name" with the object
		 * passed in.
		 * @param	obj		Object to replace current.
		 * @param	name	Key to store object under.
		 */
		public function replaceObject(obj:SimObject, name:String):void
		{
			_objects[name] = obj;
		}
		
		/**
		 * Remove the object associated with the specified key.
		 * @param	name	Name key of the object.
		 */
		public function removeObjectByName(name:String):void
		{
			delete _objects[name];
		}
		
		/**
		 * Remove the object specified.
		 * @param	obj	Object reference to remove.
		 */
		public function removeObject(obj:SimObject):void
		{
			for each (var key:Object in _objects) {
				if (_objects[key] == obj) {
					delete _objects[key];
				}
			}
			
		}
		
	}
	
}